package com.sskj.mine.ui.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.MineRegisterActivityPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.MINE_REGISTER)
public class MineRegisterActivity extends BaseActivity<MineRegisterActivityPresenter> {
    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.login_account)
    EditText loginAccount;
    @BindView(R2.id.register_code)
    EditText registerCode;
    @BindView(R2.id.get_code)
    TextView getCode;
    @BindView(R2.id.register_pwd)
    EditText registerPwd;
    @BindView(R2.id.invite_code)
    EditText inviteCode;
    @BindView(R2.id.register_btn)
    Button registerBtn;
    @BindView(R2.id.login_tv)
    TextView loginTv;
    private Disposable timeDis;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_register;
    }

    @Override
    public MineRegisterActivityPresenter getPresenter() {
        return new MineRegisterActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ClickUtil.click(leftImg, () -> {
            finish();
        });
        ClickUtil.click(registerBtn, () -> {
            String loginPhone = loginAccount.getText().toString();
            String code = registerCode.getText().toString();
            String pwd = registerPwd.getText().toString();
            String inviCode = inviteCode.getText().toString();
            if (TextUtils.isEmpty(loginPhone)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineRegisterActivity1_j));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineRegisterActivity2_j));
                return;
            }
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineRegisterActivity3_j));
                return;
            }
            mPresenter.register(loginPhone, code, pwd, pwd, inviCode);
        });
        ClickUtil.click(loginTv, () -> {
            finish();
        });
        ClickUtil.click(getCode, () -> {
            String loginPhone = loginAccount.getText().toString();
            if (TextUtils.isEmpty(loginPhone)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineRegisterActivity1_j));
                return;
            }
            getCode.setEnabled(false);
            mPresenter.getCode(loginPhone, "1");
        });
    }


    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        getCode.setText(aLong + "s");
                    } else {
                        getCode.setText(App.INSTANCE.getString(R.string.mine_mineRegisterActivity4_j));
                        getCode.setEnabled(true);
                    }
                });
    }

    public void sendCheckCodeFail() {
        getCode.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void registerSuccess() {
        finish();
    }

}
